package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.exception.BusinessException;
import com.itheima.interceptor.UserHolder;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserApi userApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //获取用户id
        Long userId = UserHolder.getUserId();
        //获取用户的手机号
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    //设置陌生人问题，保存
    public void saveQuestion(String content) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //判断question是否为空
        if (question == null) {
            //如果不存在，保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //如果存在，修改
            question.setTxt(content);
            questionApi.update(question);
        }


    }

    //通知设置
    public void saveSettings(Map map) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //获取当前用户的通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //判断数据库是否存在数据
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification((Boolean) map.get("likeNotification"));
            settings.setGonggaoNotification((Boolean) map.get("gonggaoNotification"));
            settings.setPinglunNotification((Boolean) map.get("pinglunNotification"));
            settingsApi.save(settings);
        } else {
            settings.setLikeNotification((Boolean) map.get("likeNotification"));
            settings.setGonggaoNotification((Boolean) map.get("gonggaoNotification"));
            settings.setPinglunNotification((Boolean) map.get("pinglunNotification"));
            settingsApi.update(settings);
        }
    }

    //黑名单，列表
    public PageResult blacklist(int page, int pagesize) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //调用api查询用户黑名单分页列表，Ipage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, pagesize);
        //将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult( pagesize, page, iPage.getTotal(),iPage.getRecords());
        //返回
        return pr;
    }

    //取消黑名单
    public void deleteBlacklist(Long blackUserId) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //调用api删除
        blackListApi.delete(userId,blackUserId);
    }

    //修改手机号，校验验证码
    public boolean checkVerificationCode(String verificationCode) {
        //获取用户手机号
        String phone = UserHolder.getMobile();
        //从redis中获取下发的验证码
        String redisCode = redisTemplate.opsForValue().get(phone);
        //对验证码进行校验
        boolean verification = false;
        if (StringUtils.isNotEmpty(redisCode)&&redisCode.equals(verificationCode)){
            verification = true;
        }else{
            throw new BusinessException(ErrorResult.loginError());
        }
        //删除redis中的验证码
        redisTemplate.delete(phone);

        return verification;
    }

    //更新手机号
    public void updatePhone(String phone) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);
        userApi.update(user);
    }
}
